# -*- coding:utf-8 -*-
'''
输出所有和为S的连续正数序列。序列内按照从小至大的顺序，序列间按照开始数字从小到大的顺序

解题思路：笨方法，子序列一定位于前半序列，所以先减少搜索空间，然后计算和是否先等和大于tsum就减少y
'''
class Solution:
    def FindContinuousSequence(self, tsum):
        i = 0
        if tsum == 1:
            return []
        if tsum%2 == 0:
            mid = int(tsum/2)
        else:
            mid = int(tsum/2)+1
        stack = []
        res = []
        for a in range(1, tsum+1):
            stack.append(a)
        j = mid
        while i < j and i < mid:
            if sum(stack[i:j+1]) > tsum:
                j -= 1
            elif sum(stack[i:j+1]) < tsum:
                i += 1
                j = mid
            else:
                res .append(stack[i:j+1])
                i += 1
                j = mid
        return res

        # res=[]
        # for i in range(1,tsum//2+1):
        #     sumRes=i
        #     for j in range(i+1,tsum//2+2):
        #         sumRes+=j
        #         if sumRes==tsum:
        #             res.append(list(range(i,j+1)))
        #             break
        #         elif sumRes>tsum:
        #             break
        # return res
s = Solution()
print(s.FindContinuousSequence(1))